import express from "express";
const router = express.Router();
import { ValidationError } from "../../utils/errors";
import { formatResponse } from "../../utils/tools";
import { LoginService } from "../../services/system/LoginService";
import { SysUserService } from "../../services/system/SysUserService";
import { SysMenuService } from "../../services/system/SysMenuService";

router.post("/login", async (req, res) => {
  if (!req.body.loginId || !req.body.loginPwd) {
    throw new ValidationError("请输入账号密码");
  }

  const result = await LoginService.login(req.body, req);
  res.send(formatResponse(result));
});

router.get("/getinfo", async (req: any, res) => {
  const result = await SysUserService.getInfo(req.token.id);
  const body = formatResponse(result);
  res.send(body);
});

router.post("/logout", async (_req: any, res) => {
  res.send(formatResponse(true));
});

router.get("/getRouters", async (req: any, res) => {
  const result = await SysMenuService.findByFrontEndRouter(req.token);
  res.send(formatResponse(result));
});

export default router;
